package routes

import (
	"github.com/gin-contrib/cors"
	"github.com/gin-gonic/gin"
	_ "server/model/database"
	"server/routes/picture"
	"time"
)

var router = gin.Default()


//func Cors() gin.HandlerFunc {
//	return func(c *gin.Context) {
//		origin := c.Request.Header.Get("origin")	//请求头部
//		if len(origin) == 0 {
//			origin = c.Request.Header.Get("Origin")
//		}
//		//接收客户端发送的origin （重要！）
//		c.Writer.Header().Set("Access-Control-Allow-Origin", origin)
//		//允许客户端传递校验信息比如 cookie (重要)
//		c.Writer.Header().Set("Access-Control-Allow-Credentials", "true")
//		c.Writer.Header().Set("Access-Control-Allow-Headers", "Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token, Authorization, accept, origin, Cache-Control, X-Requested-With")
//		//服务器支持的所有跨域请求的方法
//		c.Writer.Header().Set("Access-Control-Allow-Methods", "OPTIONS, GET, POST, PUT, DELETE, UPDATE")
//		c.Writer.Header().Set("Content-Type", "application/json; charset=utf-8")
//		// 设置预验请求有效期为 86400 秒
//		c.Writer.Header().Set("Access-Control-Max-Age", "86400")
//		if c.Request.Method == "OPTIONS" {
//			c.AbortWithStatus(204)
//			return
//		}
//		c.Next()
//	}
//}

func Run() {

	var config = cors.Config{
		AllowAllOrigins:  true,
		AllowMethods:     []string{"GET", "POST", "PUT", "DELETE", "OPTIONS"},
		AllowHeaders:     []string{"Origin", "Content-Length", "Content-Type","*"},
		ExposeHeaders:    []string{"Content-Length", "text/plain", "Authorization", "Content-Type"},
		AllowCredentials: true,
		MaxAge:           12 * time.Hour,
	}
	//router.Use(Cors())
router.Use(cors.New(config))
	genRoutes()
	router.Run(":5000")
}

func genRoutes() {
	var api = router.Group("/api")

	picture.GerRoute(api)
}
